<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" />
      </template>
    </BasicForm>

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    >
      <template #tableTitle>
        <n-button type="primary" @click="addTable">
          <template #icon>
            <n-icon>
              <PlusOutlined />
            </n-icon>
          </template>
          发布商品
        </n-button>
      </template>

      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getGoodsPage, deleteGoods } from '@/api/goods/goods';
  //import { getGoodsClassifyList } from '@/api/goods/goodsClassify';
  import { columns } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import { ResultEnum } from '@/enums/httpEnum';
  import { useRouter } from 'vue-router';

  const router = useRouter();

  const schemas: FormSchema[] = [
    // {
    //   field: 'goodsClassifyId',
    //   component: 'NInput',
    //   label: '商品分类',
    //   rules: [{ required: false }],
    // },
    {
      field: 'goodsName',
      component: 'NInput',
      label: '商品名称',
      rules: [{ required: false }],
    },
    {
      field: 'goodsSn',
      component: 'NInput',
      label: '商品编号',
      rules: [{ required: false }],
    },
    {
      field: 'goodsStatus',
      component: 'NSelect',
      label: '商品状态',
      componentProps: {
        placeholder: '请选择状态',
        options: [
          {
            label: '下架',
            value: 0,
          },
          {
            label: '上架',
            value: 1,
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: false }],
    },
    {
      field: 'goodsType',
      component: 'NSelect',
      label: '商品类型',
      componentProps: {
        placeholder: '请选择商品类型',
        options: [
          {
            label: '普通商品',
            value: 0,
          },
          {
            label: '抢购商品',
            value: 1,
          },
          {
            label: '盲盒商品',
            value: 2,
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: false }],
    },
  ];
  const message = useMessage();
  const actionRef = ref();
  const dialog = useDialog();
  const params = ref({});

  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
          },
          {
            label: '删除',
            type: 'error',
            icon: 'ic:outline-delete-outline',
            onClick: handleDelete.bind(null, record),
          },
        ],
      });
    },
  });

  const [register, {}] = useForm({
    //gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    gridProps: { cols: '2 s:2 m:3 l:5 xl:6 2xl:6' },
    labelWidth: 80,
    schemas,
  });

  function addTable() {
    router.push({ name: 'goodsCreate' });
  }

  const loadDataTable = async (res) => {
    return await getGoodsPage({ ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    router.push({
      name: 'goodsCreate',
      params: { id: record.id, title: `编辑商品-${record.goodsName}` },
    });
  }

  function handleDelete(record: Recordable) {
    console.log('点击了删除', record);
    dialog.info({
      title: '提示',
      content: `您确定想删除吗?`,
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        const params = { id: record.id };
        const response = await deleteGoods(params);
        const { message: msg, code } = response;
        if (code == ResultEnum.SUCCESS) {
          message.success(msg);
          reloadTable();
        } else {
          message.error(msg);
        }
      },
      onNegativeClick: () => {
        //message.error('已取消');
      },
    });
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    params.value = values;
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
</script>

<style lang="less" scoped></style>
